package com.soulmate.cart.controller;

import common.pojo.Cart;
import common.vo.SysResult;
import com.soulmate.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart/manage")
public class CartController {
    @Autowired
    private CartService cartService=null;

    /**
     * 根据 userId查询购物车列表
     * @param userId
     * @return
     */
    @RequestMapping("/query")
    public List<Cart> findCartsByUserId(String userId){
        List<Cart> cartList= cartService.findCartsByUserId(userId);
        return cartList;
    }

    /**
     * 添加一个商品到购物车
     * @param cart
     * @return
     */
    @RequestMapping("/save")
    public SysResult addCart(Cart cart){
        try {
            cartService.addCart(cart);
            return SysResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(400,"fail",null);
        }
    }

    /**
     * 更新商品数量
     * @param cart
     * @return
     */
    @RequestMapping("/update")
    public SysResult updateNum(Cart cart){
        try {
            cartService.updateNum(cart);
            return SysResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(400,"fail",null);
        }
    }

    /**
     * 删除用户购物车中指定商品
     * 通过userId productId两个字段定位
     * @param cart
     * @return
     */
    @RequestMapping("/delete")
    public SysResult deleteCart(Cart cart){
        try {
            cartService.deleteCart(cart);
            return SysResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(400,"fail",null);
        }
    }


}
